package com.itheima.cs.dao;


import com.itheima.cs.entity.ScanInStorage;
import com.itheima.cs.help.DbOpenHelper;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ScanInStorageDao extends DbOpenHelper {

    public int addPurchaseOrder(ScanInStorage scanInStorage) {
        int iRow = 0;
        try {
            getConnection();
            String sql = "INSERT INTO jdscrk (document_type, entry_time, goods_number, goods_name, specifications_model, receipt_type, unit, receivable_quantity, received_quantity, warehouse, production_workshop) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, scanInStorage.getDocumentType());
            pStmt.setTimestamp(2, new Timestamp(scanInStorage.getEntryTime().getTime()));
            pStmt.setString(3, scanInStorage.getGoodsNumber());
            pStmt.setString(4, scanInStorage.getGoodsName());
            pStmt.setString(5, scanInStorage.getSpecificationsModel());
            pStmt.setString(6, scanInStorage.getReceiptType());
            pStmt.setString(7, scanInStorage.getUnit());
            pStmt.setString(8, scanInStorage.getReceivableQuantity());
            pStmt.setString(9, scanInStorage.getReceivedQuantity());
            pStmt.setString(10, scanInStorage.getWarehouse());
            pStmt.setString(11, scanInStorage.getProductionWorkshop());
            iRow = pStmt.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return iRow;
    }


    public List<ScanInStorage> getAllProductionOrdersList(){
        List<ScanInStorage> list = new ArrayList<>();
        try{
            getConnection();
            String sql = "select * from jdscrk";
            pStmt = conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ScanInStorage item = new ScanInStorage();
                item.setDocumentNumber(rs.getString("document_number"));
                item.setGoodsNumber(rs.getString("goods_number"));

                list.add(item);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            closeAll();
        }
        return list;
    }
}
